package com.jhsw.roadsun.module.business.service.expressagegoods;

import com.jhsw.roadsun.framework.common.pojo.PageResult;
import com.jhsw.roadsun.framework.common.util.object.BeanUtils;
import com.jhsw.roadsun.module.business.controller.admin.expressagegoods.vo.ExpressageGoodsPageReqVO;
import com.jhsw.roadsun.module.business.controller.app.expressage.vo.goods.AppExpressageGoodsSaveReqVO;
import com.jhsw.roadsun.module.business.dal.dataobject.expressagegoods.ExpressageGoodsDO;
import com.jhsw.roadsun.module.business.dal.mysql.expressagegoods.ExpressageGoodsMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static com.jhsw.roadsun.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jhsw.roadsun.module.business.enums.ErrorCodeConstants.EXPRESSAGE_GOODS_NOT_EXISTS;

/**
 * 寄件货物信息 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class ExpressageGoodsServiceImpl implements ExpressageGoodsService {

    @Resource
    private ExpressageGoodsMapper expressageGoodsMapper;

    @Override
    public Long createExpressageGoods(AppExpressageGoodsSaveReqVO createReqVO) {
        // 插入
        ExpressageGoodsDO expressageGoods = BeanUtils.toBean(createReqVO, ExpressageGoodsDO.class);
        expressageGoodsMapper.insert(expressageGoods);

        // 返回
        return expressageGoods.getId();
    }

    @Override
    public void deleteExpressageGoods(Long id) {
        // 校验存在
        validateExpressageGoodsExists(id);
        // 删除
        expressageGoodsMapper.deleteById(id);
    }

    private void validateExpressageGoodsExists(Long id) {
        if (expressageGoodsMapper.selectById(id) == null) {
            throw exception(EXPRESSAGE_GOODS_NOT_EXISTS);
        }
    }

    @Override
    public ExpressageGoodsDO getExpressageGoods(Long id) {
        return expressageGoodsMapper.selectById(id);
    }

    @Override
    public PageResult<ExpressageGoodsDO> getExpressageGoodsPage(ExpressageGoodsPageReqVO pageReqVO) {
        return expressageGoodsMapper.selectPage(pageReqVO);
    }

}